#!/bin/bash

if [[ -f /var/run/vsm/vsm-agent.pid ]]; then
  update-rc.d -f vsm-agent remove
  sudo service vsm-agent stop
fi

if [[ -f /var/run/vsm/vsm-api.pid ]]; then
  update-rc.d -f vsm-api remove
  sudo service vsm-api stop
fi

if [[ -f /var/run/vsm/vsm-conductor.pid ]]; then
  update-rc.d -f vsm-conductor remove
  sudo service vsm-conductor stop
fi

if [[ -f /var/run/vsm/vsm-physical.pid ]]; then
  update-rc.d -f vsm-physical remove
  sudo service vsm-physical stop
fi

if [[ -f /var/run/vsm/vsm-scheduler.pid ]]; then
  update-rc.d -f vsm-scheduler remove
  sudo service vsm-scheduler stop
fi

DATE=`date "+%Y%m%d%H%M%S"`
if [[ -d /etc/vsm ]]; then
  cp -r /etc/vsm .
  rm -rf vsm/*.tar
  tar -cf vsm-$DATE.tar vsm
  rm -rf vsm
  mv vsm-$DATE.tar /etc/vsm
fi

rm -rf /etc/vsm/cluster.manifest
rm -rf /etc/vsm/server.manifest
rm -rf /etc/vsm/vsm.conf